<template>
	<view>
		<view class="page_title">
			<NavBack></NavBack>
			<view class="page_name">择偶要求</view>
			<view class="blank"></view>
		</view>

		<view class="banxin">
			<view class="information">
				<view class="item">
					<view class="label">年龄</view>
					<view class="info" @click="handlePopup('年龄')">
						<view>
							{{spouse.minAge}}-{{spouse.maxAge}}岁
						</view>
						<image class="next" src="/static/img/open.png" mode="" />
					</view>
				</view>
				<view class="item">
					<view class="label">身高</view>
					<view class="info" @click="handlePopup('身高')">
						<view>{{spouse.minHeight}}-{{spouse.maxHeight}}cm</view>
						<image class="next" src="/static/img/open.png" mode="" />
					</view>
				</view>
				<view class="item">
					<view class="label">月收入</view>
					<view class="info" @click="handlePopup('月收入')">
						<view>{{spouse.income}}</view>
						<image class="next" src="/static/img/open.png" mode="" />
					</view>
				</view>
				<view class="item">
					<view class="label">学历</view>
					<view class="info" @click="handlePopup('学历')">
						<view>{{spouse.education}}</view>
						<image class="next" src="/static/img/open.png" mode="" />
					</view>
				</view>
			</view>
		</view>

		<!-- 修改用户信息弹窗 -->
		<u-popup closeable class="info-popup" :show="showPopup" mode='bottom' @close="closePopup">
			<view class="my-popup">
				<view class="income_title">
					{{popupType}}
				</view>
				<view class="age" v-if="popupType=='年龄'">
					<view class="label">
						<text>年龄</text> <text class="label1">{{ageString}}</text>
					</view>
					<SliderRange :min="18" :max="50" :value="ageRange" range :bar-height="5"
						active-color="rgba(156, 156, 255, 1)" @change="dragAge" />
					<view class="tip">
						<text>18</text> <text>50</text>
					</view>
				</view>
				<view class="height" v-if="popupType=='身高'">
					<view class="label">
						<text>身高（cm）</text> <text class="label1">{{heightString}}</text>
					</view>
					<SliderRange :min="160" :max="200" :value="heightRange" range :bar-height="5"
						active-color="rgba(156, 156, 255, 1)" @change="dragHeight" />
					<view class="tip">
						<text>160</text> <text>200</text>
					</view>
				</view>
				<picker-view v-if="popupType=='月收入'" @change="changeIncome" :value="incomeValue" class="my-picker-view">
					<picker-view-column>
						<view class="item" v-for="(item,index) in incomeList" :key="index">{{item}}</view>
					</picker-view-column>
				</picker-view>
				<view class="box_list" v-if="popupType=='学历'">
					<view v-for="(item,index) in educationList" class="box" :class="item.active?'active':''"
						@click="handleEducation(index)">
						{{item.name}}
					</view>
				</view>
				<div class="confirm">
					<button type="primary" @click="confirmPopup">确认</button>
				</div>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import NavBack from '@/components/NavBack.vue'
	import SliderRange from '@/components/primewind-sliderrange/components/primewind-sliderrange/index.vue'

	export default {
		components: {
			NavBack,
			SliderRange
		},
		data() {
			return {
				spouse: {},
				showPopup: false,
				popupType: '',
				ageRange: [23, 28],
				ageString: '',
				heightRange: [170, 175],
				heightString: '',
				showIncome: false,
				incomeList: ['2000及以下', '2000-4000元', '4000-6000元', '6000-8000', '8000及以上', '不限'],
				income: '',
				educationList: [{
						name: '中专',
						active: false
					},
					{
						name: '大专',
						active: false
					},
					{
						name: '本科',
						active: false
					},
					{
						name: '硕士',
						active: false
					},
					{
						name: '博士',
						active: false
					}
				],
				education: '',
				incomeValue: [2]
			};
		},
		onLoad() {
			this.initInfo()
			this.ageString = this.ageRange.join('-'),
				this.heightString = this.heightRange.join('-'),
				this.income = this.incomeList[2]
		},
		methods: {
			dragAge(event) {
				console.log(event, 'dragAge');
				this.ageRange = event
				this.ageString = this.ageRange.join('-')
			},
			dragHeight(event) {
				console.log(event, 'dragHeight');
				this.heightRange = event
				this.heightString = this.heightRange.join('-')
			},
			changeIncome(event) {
				let current = event.detail.value[0]
				console.log(current, 'changeHouse');
				this.incomeValue[0] = current
				this.income = this.incomeList[current]
			},
			handleEducation(index) {
				this.educationList[index].active = !this.educationList[index].active
				let education = this.educationList.filter(item => {
					return item.active
				}).map(item => {
					return item.name
				})
				this.education = education.join(' | ')
				console.log(this.education, 'education');
			},
			handlePopup(type) {
				this.showPopup = true,
					this.popupType = type
			},
			closePopup() {
				this.showPopup = false
			},
			confirmPopup() {
				let spouseInfo = {}
				this.showPopup = false
				if (this.popupType == '年龄') {
					spouseInfo = {
						minAge: this.ageRange[0],
						maxAge: this.ageRange[1]
					}
					this.changeInfo(spouseInfo)
				} else if (this.popupType == '身高') {
					spouseInfo = {
						minHeight: this.heightRange[0],
						maxHeight: this.heightRange[1]
					}
					this.changeInfo(spouseInfo)
				} else if (this.popupType == '月收入') {
					spouseInfo = {
						income: this.income
					}
					this.changeInfo(spouseInfo)
				} else if (this.popupType == '学历') {
					spouseInfo = {
						education: this.education
					}
					this.changeInfo(spouseInfo)
				}

			},
			changeInfo(spouse) {
				request(`/employee/insertBaseInfo`, {
					method: 'POST',
					data: {
						spouse
					}
				}).then((res) => {
					if (res.data.code == 1) {
						this.initInfo()
						uni.showToast({
							title: '修改成功',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			initInfo() {
				let that = this
				request(`/employee/${uni.getStorageSync('id')}`).then((res) => {
					// console.log(res.data.data,'authentication');
					this.spouse = res.data.data.spouse
					this.ageRange = [this.spouse.minAge, this.spouse.maxAge]
					this.heightRange = [this.spouse.minHeight, this.spouse.maxHeight]
					console.log(this.spouse, 'spouse');
				})
			},
		}
	}
</script>

<style lang="scss">
	.information {
		border-radius: 40rpx;
		margin-top: 30rpx;
		padding: 30rpx;
		background-color: rgba(255, 255, 255, 1);
		;

		.item {
			display: flex;
			flex: 1;
			margin-bottom: 40rpx;
			align-items: center;
			justify-content: space-between;

			.label {
				font-size: 28rpx;
				color: rgba(51, 51, 51, 1);
				line-height: 40rpx;
			}

			.info {
				// font-size: 28rpx;
				display: flex;
				flex: 1;
				justify-content: flex-end;
				color: rgba(166, 166, 166, 1);
				line-height: 40rpx;
				align-items: center;

				view {
					font-size: 28rpx;
				}

				.avatar {
					width: 48rpx;
					height: 48rpx;
					border-radius: 50%;
				}

				.next {
					width: 24rpx;
					height: 24rpx;
					margin-left: 10rpx;
				}
			}
		}

		.loginOut {
			color: rgba(107, 107, 255, 1);
			background-color: rgba(235, 235, 255, 1) !important;
		}
	}

	.info-popup /deep/ .u-popup__content {
		.my-popup {
			height: 500rpx;
		}

		.income_title {
			font-size: 36rpx;
			font-weight: 500;
			text-align: center;
			margin-top: 30rpx;
		}

		button {
			width: 65%;
			position: absolute;
			bottom: 30rpx;
			left: 0;
			right: 0;
		}
	}

	.age,
	.height,
	.income,
	.education {
		width: 80%;
		margin: 80rpx auto;
		font-weight: 500;

		.label {
			font-weight: 500;
		}

		.label,
		.tip {
			display: flex;
			margin-top: 20rpx;
			justify-content: space-between;
		}

		.label1 {
			color: rgba(102, 102, 102, 1);
		}

		.slider-range /deep/ .slider-range-inner {
			height: 50rpx !important;
			// margin-top: 50rpx;
		}

		.tip {
			text {
				font-size: 28rpx;
				color: rgba(166, 166, 166, 1);
			}
		}

		.open {
			display: flex;
			align-items: center;

			image {
				width: 32rpx;
				height: 32rpx;
			}
		}
	}

	.box_list {
		width: 85%;
		display: flex;
		flex-wrap: nowrap;
		margin: 0 auto;
		justify-content: space-between;
		margin-top: 150rpx;

		.box {
			color: rgba(102, 102, 102, 1);
			font-size: 28rpx;
			padding: 10rpx 30rpx;
			margin: 0 5rpx;
			border-radius: 20rpx;
			background-color: #EDEDED;
		}

		.active {
			color: rgba(156, 156, 255, 1);
			background-color: #EBEBFF;
		}
	}

	.my-picker-view {
		width: 750rpx;
		height: 300rpx;
		margin-top: 20rpx;
	}

	.item {
		line-height: 100rpx;
		text-align: center;
	}
</style>